/*
**  Copyright (C), CHENGDU GONGWEI TECHNOLOGY CO.,LTD.
**
**  Project:	Manager Unit v1.0
**  File:    	
**  Author: 	zhaozhuyu
**  Date:    	2017/05/20
**
**  Purpose:
**    		.
**
**  History: 
**  <author>   <time>          <version >   <desc>
*	$$Author$$
*/


/* Include files. */
#include "standard.h"
#include "debug.h"

/* Local function declarations. */

/* Macro API definitions. */

/* Global variable declarations. */


/* Type definitions. */

/* Local function definitions. */

void stackDump(lua_State *L, int line)
{
    int i;
    int top = lua_gettop(L);
    printf("[%04d] %p (%d)    ",line, L, top);
    for(i = 1; i <= top; i++){
        int t = lua_type(L, i);
        switch(t){
            case LUA_TSTRING:
                printf("'%s'", lua_tostring(L, i));
                break;
            case LUA_TBOOLEAN:
                printf(lua_toboolean(L, i) ? "true":"false");
                break;
            case LUA_TNUMBER:
                printf("%g", lua_tonumber(L, i));
                break;
            default:
                printf("%s", lua_typename(L, t));
                break;
        }
        printf("  ");
    }
    printf("\n");
}

void diffTime(struct timeval t1, struct timeval t2)
{
	int usec,sec;
	bool borrow = false;
	if (t2.tv_usec > t1.tv_usec) {
		usec = t2.tv_usec - t1.tv_usec;
	}
	else{
		borrow = true;
		usec = t2.tv_usec+1000000 - t1.tv_usec;
	}
	if (borrow){
		sec = t2.tv_sec-1 - t1.tv_sec;
	}
	else{
		sec = t2.tv_sec - t1.tv_sec;
	}
	ERROR("%ds, %dms\n", sec, usec/1000);
}

#if 0
void idleCallback(struct ev_loop *loop, ev_idle *w, int revents)
{
	//printf("idle_cb\n");
	usleep(1);
	//nanosleep(1);
	//sleep(10);

	//struct timespec ts;
	//ts.tv_sec = 0;
	//ts.tv_nsec = 1;
	//nanosleep(&ts, NULL);

	//struct timeval delay;
	//delay.tv_sec = 0;
	//delay.tv_usec = 1; 
	//select(0, NULL,NULL, NULL, &delay);	
}
#endif

int traceback (lua_State *L) 
{
	const char *msg = lua_tostring(L, 1);
	if (msg)
		luaL_traceback(L, L, msg, 1);
	else {
		lua_pushliteral(L, "(no error message)");
	}
	return 1;
}


